// 公共头部 uni-transition
// import myHeader from "/pages/headerfoot/pages/header.vue";
//过度动画
// import uTransition from "/uni_modules/uni-transition/components/uni-transition/uni-transition.vue"
//语言
// import Lang from '/utils/lang.js';
//cache缓存
import Cache from '@/config/cache.js';
//打帮赛方法
// import hitmatchcommon from '@/common/hitmatchcommon.js';
// 基础方法
import methodBase from "/config/base.js";

export default {
	props: {
		project_make_id: String,
		project_id: String,
	},
	components: {
		// myHeader,
		// uTransition,
	},
	data() {
		return {
			timer: {}, //时间
			dotcss: {}, //运动css
			type: 4, //1=对角运动 2= 五角  3=8字 4=之 5=圆周
			kkborder: {
				'border': '5px solid #000',
			},
			usetime: 0, //使用时间
			flashTime: 0.2, //停留时间  1 60  1/0.5=0.2   120   1/0.1=10 600
 			submitTime: 60, //一分钟提交
			countdown:0,//倒计时
			arr: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
				'U', 'V', 'W', 'X', 'Y', 'Z'
			], //题库
			screenWidth: 0, //屏幕宽度
			screenHeight: 0, //屏幕高度
			count: 0,
			direction: 1, //1=正 2=反
			z_height: 0.05, //高度间格
			z_move_height: 0, //高度
			z_rebound: 1, //1 上到下 2=下到上
			extraction_alphabet: [], //3次字母提取
			extraction_time: [], //3次时间提取
			current_alphabet: '', //当前字母
			show: false, //是否显示1
			interval:{},//倒计时
			ifmove:true, //是否显示1
			alphabeta:'',//字母a
			alphabetb:'',//字母b
			alphabetc:'',//字母c
			ifpass: false, //闯关结果弹出
			passTheResult: ['闯关失败', '闯关成功'],
			fraction:20,//分数
		}
	},
	async mounted() {
		var that = this;
		that.getScreenSize();
		if (that.project_make_id) {
			Cache.set('project_make_id', that.project_make_id)
		}
		//console.log(that.project_id);//项目
		if (that.project_id) {
			Cache.set('project_id', that.project_id)
		}
		that.countdown=that.submitTime
		that.move();
	},
	methods: {
		passTheResults(state) {
			var that = this;
			that.ifpass = true;
			var ifpassnumber=0;
			if(that.alphabeta==that.extraction_alphabet[0]){
				ifpassnumber++;
			}
			if(that.alphabetb==that.extraction_alphabet[1]){
				ifpassnumber++;
			}
			if(that.alphabetc==that.extraction_alphabet[2]){
				ifpassnumber++;
			}
			
			if(ifpassnumber=='3'){
				that.passtitle = that.passTheResult[1];
			}else{
				that.passtitle = that.passTheResult[0];
				that.fraction = 0;
			}
		},
		del(){
			var that=this;
			if(that.alphabetc.length>0){
				 that.alphabetc='';
			 }else if(that.alphabetb.length>0){
				 that.alphabetb='';
			}else if(that.alphabeta.length>0){
				that.alphabeta='';
			 }
		},
		numberkey(e){
			 var that=this;
			 if(that.alphabeta.length<=0){
				 that.alphabeta=e
			 }else if(that.alphabetb.length<=0){
				  that.alphabetb=e
			}else if(that.alphabetc.length<=0){
				 that.alphabetc=e
			 }
		},
		move() {
			var that = this;
			var latlong = [];
			that.limittime();//倒计时
			//字母提取
			that.extraction_alphabet_time()
			that.timer = setInterval(() => {
				that.movement_judgment();
				latlong = that.move_list();
				var dotCss = {
					'left': latlong[0] + 'px',
					'top': latlong[1] + 'px',
				};
				that.dotcss = dotCss;
				that.usetime++;

			}, that.flashTime * 1000);
		},
		//倒计时
		limittime() {
			var that = this;
			that.interval = setInterval(function() {
				that.countdown = methodBase.updatenumber(that.countdown)
				if (that.countdown <= 0) {
					clearInterval(that.interval);
					clearInterval(that.timer);
					that.ifmove=false;
				}
			}, 1000);
		},
		//运动判断
		movement_judgment() {
			var that = this;
			var number=1/that.flashTime;
			if (that.usetime == that.extraction_time[0]*number) {
				that.show = true;
				that.current_alphabet = that.extraction_alphabet[0];
			} else if (that.usetime == that.extraction_time[1]*number) {
				that.show = true;
				that.current_alphabet = that.extraction_alphabet[1];
			} else if (that.usetime == that.extraction_time[2]*number) {
				that.show = true;
				that.current_alphabet = that.extraction_alphabet[2];
			} else {
				that.show = false;
			}
		},
		//字母与时间提取
		extraction_alphabet_time() {
			var that = this;
			for (var i = 0; i < 3; i++) {
				that.extraction_alphabet.push(methodBase.rand_value(that.arr));
			}
			that.extraction_time.push(methodBase.rand_number(15, 5));
			that.extraction_time.push(methodBase.rand_number(30, 16));
			that.extraction_time.push(methodBase.rand_number(55, 31));
		},
		move_list() {
			var that = this;
			var xyarr = [];
			var chaX = 0;
			var chaY = 0;
			var bignumber = 0;
			var valselect = [];
			//对角运动
			if (that.type == '1') {
				var dotArr = {
					'0': [1 / 2, 1 / 2],
					'1': [3 / 4, 3 / 4],
					'2': [1 / 4, 1 / 4],
					'3': [1, 1],
					'4': [0, 0],
					'5': [1 / 2, 1 / 2],
					'6': [1 / 4, 3 / 4],
					'7': [3 / 4, 1 / 4],
					'8': [0, 1],
					'9': [1, 0],
				};
				// var number = methodBase.rand_number(9);
				var bignumber = 9;
			} else if (that.type == '2') {
				var dotArr = {
					'0': [0, 2 / 6],
					'1': [2 / 6, 2 / 6],
					'2': [4 / 6, 2 / 6],
					'3': [6 / 6, 2 / 6],

					'4': [5 / 6, 3 / 6],
					'5': [3 / 6, 4 / 6],
					'6': [0, 5 / 6],

					'7': [1 / 6, 3 / 6],
					'8': [2 / 6, 2 / 6],
					'9': [3 / 6, 1 / 6],

					'10': [4 / 6, 2 / 6],
					'11': [5 / 6, 3 / 6],
					'12': [6 / 6, 5 / 6],

					'13': [3 / 6, 4 / 6],
					'14': [1 / 6, 3 / 6],
				};
				// var number = methodBase.rand_number(9);
				var bignumber = 14;
			} else if (that.type == '3') {
				var dotArr = {
					'0': [0, 1 / 8],
					'1': [1 / 2, 4 / 8],
					'2': [2 / 2, 7 / 8],
					'3': [2 / 2, 4 / 8],

					'4': [2 / 2, 1 / 8],
					'5': [1 / 2, 4 / 8],
					'6': [0, 7 / 8],
					'7': [0, 4 / 8],

				};
				var bignumber = 7;
			} else if (that.type == '4') {
				var dotArr = [];
				var p = 0;
				if (that.z_rebound == '1') {
					if (that.direction == '1') {
						for (var i = 1; i <= 5; i++) {
							dotArr.push([i / 5, (i / 100) + that.z_move_height]);
							p++;
						}
					} else {
						for (var i = 1; i <= 5; i++) {
							dotArr.push([(5 - i) / 5, (i / 100) + that.z_move_height]);
							p++;
						}
					}
				} else {
					if (that.direction == '1') {
						for (var i = 1; i <= 5; i++) {
							dotArr.push([i / 5, that.z_move_height - (i / 100)]);
							p++;
						}
					} else {
						for (var i = 1; i <= 5; i++) {
							dotArr.push([(5 - i) / 5, that.z_move_height - (i / 100)]);
							p++;
						}
					}
				}

				var bignumber = p - 1;
				if (that.count >= bignumber) {
					if (that.z_rebound == '1') {
						that.z_move_height = that.z_height + that.z_move_height;
					} else {
						that.z_move_height = that.z_move_height - that.z_height;
					}
					if (that.z_move_height > 1) {
						that.z_rebound = 2;
					} else if (that.z_move_height < 0) {
						that.z_rebound = 1;
					}
					p = 0
					if (that.direction == '1') {
						that.direction = 2;
					} else {
						that.direction = 1;
					}
				}
			} else if (that.type == '5') {
				var dotArr = {
					'0': [2 / 4, 1 / 6],
					'1': [3 / 4, 2 / 6],
					'2': [4 / 4, 3 / 6],
					'3': [3 / 4, 4 / 6],
					'4': [2 / 4, 5 / 6],
					'5': [1 / 4, 4 / 6],
					'6': [0 / 4, 3 / 6],
					'7': [1 / 4, 2 / 6],

				};
				var bignumber = 7;
			}
			var number = that.count;
			valselect = dotArr[number];
			chaX = (valselect[0] * that.screenWidth) + 100;
			chaY = (valselect[1] * that.screenHeight) + 60;
			if (number >= bignumber) {
				that.count = 0;
			} else {
				that.count++;
			}
			xyarr = [chaX, chaY];
			return xyarr;
		},
		getScreenSize() {
			var that = this;
			uni.getSystemInfo({
				success: (res) => {
					const Width = res.windowWidth - 100; // 屏幕宽度，单位为px
					const Height = res.windowHeight - 100; // 屏幕高度，单位为px
					that.screenWidth = Width - 120; //屏幕宽度
					that.screenHeight = Height - 50; //屏幕高度
					that.kkborder = {
						'border': '5px solid #000',
						'width': Width + 'px',
						'height': Height + 'px',
						'margin': 'auto',
						'margin-top': '20px',
					};
				},
			});
		},
	},
	// 在页面的 onUnload 生命周期中停止更新位置
	onUnload() {
		var that = this;
		console.log("停止生命周期1");
		clearInterval(that.interval);
		clearInterval(that.timer);
	}
	 
}